package com.thenie.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.Dept;
import com.thenie.service.DeptService;
import com.thenie.utils.DataGridViewResult;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.DeptVo;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 后台部门控制器
 * 只返回数据
 */
@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Resource
    private DeptService deptService;

    /**
     * 查询部门列表
     * @param deptVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(DeptVo deptVo){
        //设置当前页，当前页的数量大小
        PageHelper.startPage(deptVo.getPage(),deptVo.getLimit());

        //查询部门列表
        List<Dept> deptList = deptService.findDeptListByPage(deptVo);

        PageInfo<Dept> deptPageInfo = new PageInfo<Dept>(deptList);
        return new DataGridViewResult(deptPageInfo.getTotal(), deptPageInfo.getList());
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @RequestMapping("/addDept")
    public String addDept(Dept dept){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = deptService.addDept(dept);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @RequestMapping("/updateDept")
    public String updateDept(Dept dept){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = deptService.updateDept(dept);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping("/deleteDept")
    public String deleteDept(int id){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =deptService.deleteDeptById(id);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询部门list集合
     * @return
     */
    @RequestMapping("findDeptList")
    public String findDeptList(){
        List<Dept> deptList = deptService.findDeptListByPage(new DeptVo());
        return JSON.toJSONString(deptList);
    }
}
